package com.funnyx.service.baseservice;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.MainResponse;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Map;

/**
 * Elasticsearch 搜索 service，{@link RestHighLevelClient}提供了同步和异步两种实现，在这里先对客户端提供同步方法
 *
 * @author funnyx
 */
@Slf4j
public class GetService {

  @Autowired private RestHighLevelClient restHighLevelClient;

  /**
   * 是否和Elasticsearch联通
   *
   * @return true -是，false -fou
   */
  public boolean ping() {
    try {
      return restHighLevelClient.ping(RequestOptions.DEFAULT);
    } catch (Exception e) {
      log.error("ping() has an error! {}", e.getMessage());
    }
    return false;
  }

  public String info() {
    try {
      MainResponse info = restHighLevelClient.info(RequestOptions.DEFAULT);
      return info.getClusterName();
    } catch (IOException e) {
      log.error("info() has an error! {}", e.getMessage());
    }
    return "";
  }

  /**
   * 这里是返回restHighLevelClient
   *
   * @param getRequest {@link GetRequest} , {@link RequestOptions} 查询配置参数，使用默认配置即可，如需自定义请自行修改
   * @return 返回为高阶客户端提供的封装对象，客户端可自行使用
   */
  public GetResponse get(GetRequest getRequest) {
    try {
      return restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
      log.error(e.getMessage());
    }
    return null;
  }

  /**
   * 以字符串的形式返回文档
   *
   * @param getRequest
   * @return
   */
  public String getAsString(GetRequest getRequest) {
    GetResponse response = get(getRequest);
    if (response.isExists()) {
      return response.getSourceAsString();
    } else return null;
  }

  /**
   * 以Map的形式返回文档
   *
   * @param getRequest
   * @return
   */
  public Map<String, Object> getAsMap(GetRequest getRequest) {
    GetResponse response = get(getRequest);
    if (response.isExists()) {
      return response.getSourceAsMap();
    } else {
      return null;
    }
  }

  /**
   * 提供 索引名称，索引类型，文档ID 来检索文档数据
   *
   * @param index 索引名称
   * @param type 索引类型
   * @param docId 文档ID
   * @return
   */
  public GetResponse get(String index, String type, String docId) {
    GetRequest request = new GetRequest(index, type, docId);
    return get(request);
  }

  /**
   * 提供 索引名称，索引类型，文档ID 来检索文档数据
   *
   * @param index 索引名称
   * @param type 索引类型
   * @param docId 文档ID
   * @return 字符串
   */
  public String getAsString(String index, String type, String docId) {
    GetRequest request = new GetRequest(index, type, docId);
    GetResponse response = get(request);
    if (response.isExists()) {
      return response.getSourceAsString();
    } else {
      return null;
    }
  }

  /**
   * 提供 索引名称，索引类型，文档ID 来检索文档数据
   *
   * @param index 索引名称
   * @param type 索引类型
   * @param docId 文档ID
   * @return Map
   */
  public Map<String, Object> getAsMap(String index, String type, String docId) {
    GetRequest request = new GetRequest(index, type, docId);
    GetResponse response = get(request);
    if (response.isExists()) {
      return response.getSourceAsMap();
    } else {
      return null;
    }
  }

  /**
   * true- if the document exists, false- otherwise
   *
   * @param getRequest
   * @return
   */
  public boolean exists(GetRequest getRequest) {
    try {
      return restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
      log.error(e.getMessage());
    }
    return false;
  }
}
